
package org.example.leetcode;

/**
 * @version 1.0.0
 * @author: lynn
 * @description: 最长公共前缀
 * @date: 2021/7/25 16:45
 */
public class LC14 {
    public static void main(String[] args) {
        LC14 lc=new LC14();
        String[] strs={"ab","a"};
        System.out.println(lc.longestCommonPrefix(strs));
    }
    //fixme https://www.bilibili.com/video/BV1ft4y1i7Y8?from=search&seid=2012614588606344853
    public String longestCommonPrefix(String[] strs) {
        if (strs.length==0) return "";
        if (strs.length==1) return strs[0];
        String res=strs[0];
        for (int i = 1; i <strs.length ; i++) {
            res=longestCommonPrefix(res,strs[i]);
        }
        return res;
    }

    private String longestCommonPrefix(String s1,String s2){
        int length=Math.min(s1.length(),s2.length());
        int i=0;
        for ( ; i <length ; i++) {
            if (s1.charAt(i)==s2.charAt(i)){
                continue;
            }else {
                break;
            }
        }
        return s1.substring(0,i);
    }
}
